package ufc.sd.dao;

import java.util.ArrayList;
import java.util.List;

import me.prettyprint.cassandra.model.CqlQuery;
import me.prettyprint.cassandra.model.CqlRows;
import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.beans.Row;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.QueryResult;
import ufc.sd.entities.Curso;

public class CursoDAO extends CassandraDAO<Curso> {

	protected static final String CF_CURSO = "Curso";
	private static final String COLUNA_NOME;
	private static final String COLUNA_CENTRO;
	private static ColumnFamilyTemplate<String, String> cursoTemplate;
	private static CursoDAO instance;

	static {
		COLUNA_NOME = "nome";
		COLUNA_CENTRO = "centro";
	}

	public static CursoDAO getInstance() {
		if (instance == null) {
			instance = new CursoDAO();
		}
		return instance;
	}

	private CursoDAO() {
		cursoTemplate = new ThriftColumnFamilyTemplate<String, String>(keyspace, CF_CURSO, stringSerializer, stringSerializer);
	}

	private ColumnFamilyResult<String, String> queryResult;

	@Override
	public void insert(Curso curso) throws HectorException {
		Mutator<String> mutatorCurso = HFactory.createMutator(keyspace, stringSerializer);
		mutatorCurso.addInsertion(curso.getCodigo(), CF_CURSO, HFactory.createStringColumn("nome", curso.getNome()));
		mutatorCurso.addInsertion(curso.getCodigo(), CF_CURSO, HFactory.createStringColumn("centro", curso.getCentro()));

		mutatorCurso.execute();
	}

	@Override
	public List<Curso> getAll() throws HectorException {
		CqlQuery<String, String, String> cqlQuery = new CqlQuery<String, String, String>(keyspace, stringSerializer,
				stringSerializer, stringSerializer);
		cqlQuery.setQuery("select * from Curso");
		QueryResult<CqlRows<String, String, String>> result = cqlQuery.execute();
		List<Curso> cursos = new ArrayList<Curso>();
		if (result.get() == null) {
			return cursos;
		}
		List<Row<String, String, String>> allRows = result.get().getList();

		for (Row<String, String, String> actualRow : allRows) {
			Curso curso = new Curso();
			curso.setCodigo(actualRow.getKey());
			curso.setNome(actualRow.getColumnSlice().getColumnByName((COLUNA_NOME)).getValue());
			curso.setCentro(actualRow.getColumnSlice().getColumnByName((COLUNA_CENTRO)).getValue());
			cursos.add(curso);
		}
		return cursos;
	}

	@Override
	public Curso getByKey(String key) throws HectorException {
		Curso cursoResult = new Curso();

		queryResult = cursoTemplate.queryColumns(key);

		cursoResult.setCodigo(key);
		cursoResult.setNome(queryResult.getString(COLUNA_NOME));
		cursoResult.setCentro(queryResult.getString(COLUNA_CENTRO));
		return cursoResult;
	}

	@Override
	public void delete(Curso entity) {
		Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
		mutator.addDeletion(entity.getCodigo(), CF_CURSO, null, stringSerializer);
		mutator.execute();
	}

}
